/*
 * QueryPanel.java
 *
 * Created on 5. Juni 2008, 19:12
 */

package twi.view;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Selector;
import com.hp.hpl.jena.rdf.model.Statement;

import twi.OWLModel;
import twi.QueryManager;
import twi.filter.DomainRangeFilter;
import twi.filter.Filter;
import twi.filter.TypeFilter;
import twi.filter.TypeNodeFilter;
import twi.query.SparqlQuery;
import twi.result.QueryResult;
import twi.result.table.RTable;

/**
 *
 * @author  Soeren
 */
public class QueryPanel extends javax.swing.JPanel {

    
    public static void main(String[] arg){
        JFrame jf = new JFrame();
        jf.add(new QueryPanel());
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
    }
    ResultView[] views = {
      new PrefuseGraphViewDemo(),
      new PrefuseTreeViewDemo("name")
    };
    /** Creates new form QueryPanel */
    public QueryPanel() {
        initComponents();
        
        for (int i = 0; i < views.length; i++)
            this.jTabbedPane1.add(views[i].getVisualComponent(), views[i].getName());
        for (int i = 0; i < views.length; i++)
            views[i].setup();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jSplitPane1.setRightComponent(jTabbedPane1);

        jPanel1.setName("jPanel1"); // NOI18N

        jButton1.setText("update");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(178, 178, 178)
                .addComponent(jButton1)
                .addContainerGap(326, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 687, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
        	final OWLModel model = OWLModel.readOWLFile(new File("TWI6.owl"));
        	twi.filter.Filter f = new Filter(){
        		
				@Override
				public boolean isValid(RDFNode node) {
	
					if (RTable.getNodeName(node).toLowerCase().contains("volker"))
						return false;
					return true;
				}
				public void decodeString(String filterString) {	
				}
				public String encodeString() {
					return null;
				}
        	};
        	
            String sparql = " SELECT ?sub ?pre ?obj \n WHERE { ?sub ?pre ?obj. } ";
            Property p = model.getProperty("twi", "hatModul");
            SparqlQuery query = new SparqlQuery(sparql, model);
            query.addFilter(f);
//            query.addFilter(new TypeFilter(true));
            query.addNodeFilter(new TypeNodeFilter(true));
            query.addFilter(new DomainRangeFilter(true));
            QueryResult qrs = QueryManager.executeQuery(query);
            String[] title = qrs.asTable().getTitleRow();
            for (int i = 0; i < title.length; i++) {
                System.out.print(title[i] + "  |  ");
            }
            System.out.println();
            String[][] val = qrs.asTable().getValues(true);
            for (int i = 0; i < val.length; i++) {
                for (int j = 0; j < val[i].length; j++) {
                    System.out.print(val[i][j] + "  |  ");
                }//GEN-LAST:event_jButton1ActionPerformed
                System.out.println();
            }
            
            for (int i = 0; i<  this.views.length; i++)
                this.views[i].setResult(qrs);
            
            validate();
            
        } catch (IOException ex) {
            Logger.getLogger(QueryPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
}                                        


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}
